package com.jiangyg.devtools.execute.code;

import com.intellij.openapi.project.Project;
import com.intellij.ui.components.JBScrollPane;
import com.jiangyg.devtools.entity.code.BeanDefinition;
import com.jiangyg.devtools.entity.code.BeanProperty;
import com.jiangyg.devtools.ui.table.AbstractEditableTableModel;
import com.jiangyg.devtools.ui.table.EditableTable;

import java.util.List;

/**
 * 类描述：属性列表面板
 *
 * @author jiangyg
 * @date 2023-08-29
 */
public class PropertyListTablePanel extends JBScrollPane {

    private BeanDefinition beanDefinition;

    private final MyTableModel tableModel = new MyTableModel();

    public PropertyListTablePanel(Project project) {
        super();
        this.setViewportView(new EditableTable("property-list", tableModel, project));
    }

    /**
     * 功能描述：加载 Bean 定义数据
     *
     * @param beanDefinition Bean 定义数据
     */
    public void loadBeanDefinition(BeanDefinition beanDefinition) {
        this.beanDefinition = beanDefinition;

        // 清空列表
        tableModel.emptyAllRows();

        // 加载数据
        final List<BeanProperty> properties = beanDefinition.getProps();
        for (BeanProperty property : properties) {
            tableModel.addRow(property);
        }
    }

    public BeanDefinition getBeanDefinition() {
        beanDefinition.extractPropTypeAndImport();
        return beanDefinition;
    }

    private class MyTableModel extends AbstractEditableTableModel<BeanProperty> {

    }

}
